package com.woniu.bh.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bh.backstage.dao.AdvertisementMapper;
import com.woniu.bh.backstage.dao.AdvertisementPicMapper;
import com.woniu.bh.backstage.dao.SystemMessageMapper;
import com.woniu.bh.backstage.dto.AdDto;
import com.woniu.bh.backstage.param.AdRejectParam;
import com.woniu.bh.backstage.param.QueryAdParam;
import com.woniu.bh.backstage.service.IAdvertisementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.bh.common.core.domain.advertisement.Advertisement;
import com.woniu.bh.common.core.domain.advertisement.AdvertisementPic;
import com.woniu.bh.common.core.domain.backstage.SystemMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxb
 * @since 2020-10-12
 */
@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement> implements IAdvertisementService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Autowired
    private AdvertisementPicMapper advertisementPicMapper;

    @Autowired
    private SystemMessageMapper systemMessageMapper;

    @Override
    public IPage<AdDto> selectPage(QueryAdParam queryAdParam) {
        System.out.println(advertisementMapper);
        System.out.println(queryAdParam);
        System.out.println(queryAdParam.toString());
        System.out.println(queryAdParam.getAdName());
        System.out.println(queryAdParam.getDateStart());

        Page<AdDto> page = new Page<>(queryAdParam.getPageIndex(),queryAdParam.getPageSize());
        System.out.println(page.toString());
        IPage<AdDto> iPage = advertisementMapper.queryPage(page,queryAdParam.getAdName(),queryAdParam.getAdStatus(),queryAdParam.getAdOnShelf(),queryAdParam.getDateStart(),queryAdParam.getDateEnd());
//        IPage<AdDto> iPage = advertisementMapper.queryPage(page,null,null,null,null,null);
        List<AdDto> list = iPage.getRecords();
        //根据查询结果查询广告图片地址
        for (AdDto adDto:list) {
            //根据每个广告id查询所有的广告图片集合
            Integer id = adDto.getAdvertisementId();
            QueryWrapper<AdvertisementPic> wrapper = new QueryWrapper<>();
            wrapper.eq("advertisement_pic_advertisement_id",id);
            List<AdvertisementPic> list1 = advertisementPicMapper.selectList(wrapper);
            System.out.println(list1);
            List<String> pic = new ArrayList<>();
            for (AdvertisementPic adPic:list1
                 ) {
                pic.add(adPic.getAdvertisementPicAddress());
                adDto.setAdvertisementPic(pic);
            }
        }
        return iPage;
    }

    @Override
    public void auditPass(Integer id) {
        Advertisement advertisement = new Advertisement();
        advertisement.setAdvertisementId(id);
        advertisement.setAdvertisementStatus(1);
        advertisementMapper.updateById(advertisement);
    }

    @Override
    public void auditReject(AdRejectParam adRejectParam) {
        //修改广告信息为已拒绝
        Advertisement advertisement = new Advertisement();
        advertisement.setAdvertisementId(adRejectParam.getAdId());
        advertisement.setAdvertisementStatus(2);
        advertisement.setAdvertisementRefuseContent(adRejectParam.getContent());
        advertisementMapper.updateById(advertisement);
        //消息表添加该消息
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setSystemMessageContent(adRejectParam.getContent());
        systemMessage.setSystemMessageAccepterId(adRejectParam.getBsId());
        systemMessage.setSystemMessageCreatTime(System.currentTimeMillis());
        systemMessage.setSystemMessageIsChecked(0);
        systemMessage.setSystemMessageType(3);
        systemMessageMapper.insert(systemMessage);
    }

    @Override
    public IPage<AdDto> selectOnshelfPage(QueryAdParam queryAdParam) {
        Page<AdDto> page = new Page<>(queryAdParam.getPageIndex(),queryAdParam.getPageSize());
        IPage<AdDto> iPage = advertisementMapper.queryOnshelfPage(page,queryAdParam.getAdStatus(),queryAdParam.getDateStart(),queryAdParam.getDateEnd());
        List<AdDto> list = iPage.getRecords();
        //根据查询结果查询广告图片地址
        for (AdDto adDto:list) {
            //根据每个广告id查询所有的广告图片集合
            Integer id = adDto.getAdvertisementId();
            QueryWrapper<AdvertisementPic> wrapper = new QueryWrapper<>();
            wrapper.eq("advertisement_pic_advertisement_id",id);
            List<AdvertisementPic> list1 = advertisementPicMapper.selectList(wrapper);
            System.out.println(list1);
            List<String> pic = new ArrayList<>();
            for (AdvertisementPic adPic:list1
            ) {
                pic.add(adPic.getAdvertisementPicAddress());
                adDto.setAdvertisementPic(pic);
            }
        }
        return iPage;
    }

    @Override
    public IPage<AdDto> selectWaitOnshelfPage(QueryAdParam queryAdParam) {
        Page<AdDto> page = new Page<>(queryAdParam.getPageIndex(),queryAdParam.getPageSize());
        IPage<AdDto> iPage = advertisementMapper.queryWaitOnshelfPage(page,queryAdParam.getDateStart(),queryAdParam.getDateEnd());
        List<AdDto> list = iPage.getRecords();
        //根据查询结果查询广告图片地址
        for (AdDto adDto:list) {
            //根据每个广告id查询所有的广告图片集合
            Integer id = adDto.getAdvertisementId();
            QueryWrapper<AdvertisementPic> wrapper = new QueryWrapper<>();
            wrapper.eq("advertisement_pic_advertisement_id",id);
            List<AdvertisementPic> list1 = advertisementPicMapper.selectList(wrapper);
            System.out.println(list1);
            List<String> pic = new ArrayList<>();
            for (AdvertisementPic adPic:list1
            ) {
                pic.add(adPic.getAdvertisementPicAddress());
                adDto.setAdvertisementPic(pic);
            }
        }
        return iPage;
    }


}
